package com.zahb.qadx.skeleton;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.zahb.qadx.R;

import java.util.ArrayList;
import java.util.List;

import io.supercharge.shimmerlayout.ShimmerLayout;

/**
 * Created by ethanhua on 2017/7/29.
 */

public class ShimmerViewHolder extends RecyclerView.ViewHolder {

    public List<ShimmerLayout> mShimmerLayouts = new ArrayList<>();

    public ShimmerViewHolder(LayoutInflater inflater, ViewGroup parent, int innerViewResId, int shimmerMode) {
        super(inflater.inflate(shimmerMode == RecyclerViewSkeletonScreen.SHIMMER_MODE_ITEM ?
                R.layout.layout_shimmer : innerViewResId, parent, false));
        mShimmerLayouts.clear();
        if (shimmerMode == RecyclerViewSkeletonScreen.SHIMMER_MODE_ITEM) {
            ViewGroup layout = (ViewGroup) itemView;
            View view = inflater.inflate(innerViewResId, layout, false);
            /*ViewGroup.LayoutParams lp = view.getLayoutParams();
            if (lp != null) {
                layout.setLayoutParams(lp);
            }*/
            layout.addView(view);
            mShimmerLayouts.add((ShimmerLayout) itemView);
        } else if (shimmerMode == RecyclerViewSkeletonScreen.SHIMMER_MODE_ITEM_CHILD) {
            if (itemView instanceof ViewGroup) {
                getShimmerLayouts((ViewGroup) itemView);
            }
        }
    }

    private void getShimmerLayouts(ViewGroup viewGroup) {
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            if (viewGroup.getChildAt(i) instanceof ChildShimmerLayout) {
                mShimmerLayouts.add((ShimmerLayout) viewGroup.getChildAt(i));
            }
            if (viewGroup.getChildAt(i) instanceof ViewGroup) {
                getShimmerLayouts((ViewGroup) viewGroup.getChildAt(i));
            }
        }
    }
}
